package com.nmc.webapp.action;


import java.io.File;
import java.sql.Blob;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import com.nmc.common.BirthStatusEnum;
import com.nmc.service.comm.CommunicationGateway;
import com.nmc.service.manager.DeathManager;
import com.nmc.service.model.Birth;
import com.nmc.service.model.Death;
import com.nmc.service.model.Hospital;
import com.nmc.service.model.User;

/**
 * Action to allow all birth related operations.
 * 
 */
public class DeathAction extends BaseAction {
    private static final long serialVersionUID = 6558317334878272308L;
    
    /**
     * The communicationGateway
     */
    protected CommunicationGateway communicationGateway = null;
    
    private Death death;
    
    private String searchId;
    private List<Birth> birthList;
    private List<Birth> zDeliveryUpdateSearch;
    private String id;
    private Integer zoneId;
    private List<Hospital> zoneHospitalList = null;
    
    private List<String> yearsList = new ArrayList<String>();
    private List<String> religions = new ArrayList<String>();
    private List<String> attentionTypes = new ArrayList<String>();
    private List<String> deliveryType = new ArrayList<String>();
    private List<String> resPlaceTypes = new ArrayList<String>();
    
    /**
     * This will have list of hospital registered with system, last item will be others allowing to enter in case of non availibility
     */
    private List<String> birthPlaces = new ArrayList<String>();
    
    public DeathAction() {
		init();
	}
    public void init() {
    	for (int i=0; i<=70; i++){
    		yearsList.add(Integer.toString(i));
    	}
    	
    	religions.add("Hindu");
    	religions.add("Muslim");
    	religions.add("Christian");
    	religions.add("Other");
    	
    	
    	attentionTypes.add("Institutional");
    	attentionTypes.add("Medical attention other than Institutional");
    	attentionTypes.add("No Medical attention");
    	
    	resPlaceTypes.add("City");
    	resPlaceTypes.add("Town");
    	resPlaceTypes.add("Village");
    	
    }
    
	public List<String> getYearsList() {
		return yearsList;
	}
	public void setYearsList(List<String> yearsList) {
		this.yearsList = yearsList;
	}
	public List<String> getReligions() {
		return religions;
	}
	public void setReligions(List<String> religions) {
		this.religions = religions;
	}
	public String getSearchId() {
		return searchId;
	}

	public void setSearchId(String searchId) {
		this.searchId = searchId;
	}

	DeathManager deathManager;
    
    public DeathManager getDeathManager() {
		return deathManager;
	}
	public void setDeathManager(DeathManager deathManager) {
		this.deathManager = deathManager;
	}
	 public String cancel() {
	        if ("list".equals(from) && zoneId!= null) {
	            return "zBirthList";
	        } else if ("list".equals(from)){
	        	return "searchBirth";
	        }
	        return "cancel";
	    }

	/**
     * When method=GET, "input" is returned. Otherwise, "success" is returned.
     * @return cancel, input or success
     */
    public String execute() {
    	User user = userManager.getUserByUsername(getRequest().getRemoteUser());
//    	if(StringUtils.isNotEmpty(id)){
//    		if(user.getZoneId() != null){
//    			 zoneId = user.getZoneId(); 
//    		}
//    		birth = birthManager.getBirth(id);
//    	}else{
//    		Integer hospitalId = user.getHospitalId(); 
//    		if(hospitalId != null){
//    			setBirthList(birthManager.getHospitalBirthList(hospitalId));
//    		}
//    	}
         return SUCCESS;
    }

    
    /**
     * Returns "input"
     * @return "input" by default
     */
    public String doDefault() {
        return INPUT;
    }
    
    public String editRecord(){
    	return "edit";
    }

    /**
     * Save the user, encrypting their passwords if necessary
     * @return success when good things happen
     * @throws Exception when bad things happen
     */
    public String save() throws Exception {
    	HttpServletRequest request = getRequest();
        	
    	try {
			User user = userManager.getUserByUsername(request.getRemoteUser());
			
			if(death.getId() == null){
				death.setCreatedOn(new Date());
			}
			death.setUpdatedBy(user.getId());
			death.setUpdatedOn(new Date());
			death.setId(Integer.toString(new Random().nextInt()));
			
			death = deathManager.saveDeath(death);
			
			
			id= ""+death.getId();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			addActionError(getText("death.create.error" ));
			return INPUT;
		}
		
    	death = new Death();
    	saveMessage(getText("death.create.success"));
        return SUCCESS;
    }
  
   	public void setBirthList(List<Birth> birthList) {
		this.birthList = birthList;
	}

	public List<Birth> getBirthList() {
		return birthList;
	}
	
	

	public List<Birth> getzDeliveryUpdateSearch() {
		return zDeliveryUpdateSearch;
	}
	public void setzDeliveryUpdateSearch(List<Birth> zDeliveryUpdateSearch) {
		this.zDeliveryUpdateSearch = zDeliveryUpdateSearch;
	}
	public void setId(String id) {
		this.id = id;
	}

	public String getId() {
		return id;
	}
    

	public void setZoneId(Integer zoneId) {
		this.zoneId = zoneId;
	}

	public Integer getZoneId() {
		return zoneId;
	}
	public List<String> getAttentionTypes() {
		return attentionTypes;
	}
	public void setAttentionTypes(List<String> attentionTypes) {
		this.attentionTypes = attentionTypes;
	}
	public List<String> getDeliveryType() {
		return deliveryType;
	}
	public void setDeliveryType(List<String> deliveryType) {
		this.deliveryType = deliveryType;
	}
	public List<String> getResPlaceTypes() {
		return resPlaceTypes;
	}
	public void setResPlaceTypes(List<String> resPlaceTypes) {
		this.resPlaceTypes = resPlaceTypes;
	}
	public List<String> getBirthPlaces() {
		return birthPlaces;
	}
	public void setBirthPlaces(List<String> birthPlaces) {
		this.birthPlaces = birthPlaces;
	}
	public void setZoneHospitalList(List<Hospital> zoneHospitalList) {
		this.zoneHospitalList = zoneHospitalList;
	}
	public List<Hospital> getZoneHospitalList() {
		if(zoneId != null && zoneHospitalList == null){
			zoneHospitalList = hospitalManager.searchHospital(zoneId, null);
		}
		return zoneHospitalList;
	}
	public CommunicationGateway getCommunicationGateway() {
		return communicationGateway;
	}
	public void setCommunicationGateway(CommunicationGateway communicationGateway) {
		this.communicationGateway = communicationGateway;
	}
	public Death getDeath() {
		return death;
	}
	public void setDeath(Death death) {
		this.death = death;
	}

	
}
